package com.hbnu.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/*
* 使用JDBC进行数据库表的一系列功能
*加载数据库配置文件
* @author zouting 
* @Date 2025-11-04-14:42
* 
*/public class FlowerJDBCDemo01 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;

        try {
            //1.注册驱动--通过反射注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.获取数据库连接
            String url = "jdbc:mysql://localhost:3306/flowerorder?serverTimezone=GMT&useSSL=false&characterEncoding=utf-8";

            String username = "root";
            String password = "123456";
            //数据库的连接对象
            connection = DriverManager.getConnection(url, username, password);
            System.out.println(connection);

            //3.获取数据库操作对象
             statement = connection.createStatement();

            //4.执行SQL语句---用户信息添加tb_user表
//            String sql = "insert into tb_user(id,username,password,address,phone) values(01,'lisa','123456','湖北黄石','15648975')";
//            String sql = "insert into tb_user(id,username,password,address,phone) values(05,'xiaoyu','158458','湖北黄石','15658675')";
//
//            String sql =
//                    "INSERT INTO tb_user(id,username,password,address,phone) VALUES " +
//                            "(02,'zhangsan','111111','北京海淀','13800002')," +
//                            "(03,'wangwu'  ,'222222','上海浦东','13800003')," +
//                            "(04,'zhaoliu','333333','广州天河','13800004');";
//            String sql = "update tb_user set username = 'lisi' where id = 01";//根据id修改用户名更新数据暂时不用

//            int row = statement.executeUpdate(sql);
//            System.out.println( row >0  ? "添加成功":"添加失败,影响行数 = " + row);
            System.out.println("驱动加载成功");
        } catch (Exception e) {
            System.err.println("驱动加载失败: " + e.getMessage());
            e.printStackTrace();
        }finally {
            //5.释放资源
            try {
                if (statement != null){
                    statement.close();
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
            try {
                if (connection != null){
                    connection.close();
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
        }


    }
}
